package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Clue;
import com.qy.model.Member;
import com.qy.model.Message;
import com.qy.service.ClueService;
import com.github.pagehelper.PageHelper;
import com.qy.service.MemberService;
import com.qy.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/clue")
public class ClueController {
    @Resource
    private ClueService clueService;

    @Resource
    private MemberService memberService;

    @Resource
    private MessageService messageService;

    @PostMapping("/add")
    public Result add(@RequestBody Clue clue) {
        clueService.save(clue);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        clueService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Clue clue) {
        clueService.update(clue);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Clue clue = clueService.findById(id);
        return ResultGenerator.successResult(clue);
    }

    @GetMapping("/list")
    public Result list(PageBean<Clue> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize() );
        List<Clue> list = clueService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //提供线索
    @PostMapping("/affordClue")
    public Result affordClue(@RequestBody Clue clue) {
        if (clue.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Member member = memberService.findById(clue.getUser_id());
        clue.setUser_name(member.getName());
        clue.setUser_tel(member.getPhone());
        if (clue.getClue_type() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        if (clue.getClue_type() == 1) {
            if (clue.getPolicy_name() == null || clue.getPolicy_name().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getSubsidies() == null || clue.getSubsidies().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getOther_support() == null || clue.getOther_support().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getUnit_name() == null || clue.getUnit_name().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getPosition() == null || clue.getPosition().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getTel() == null || clue.getTel().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getName() == null || clue.getName().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getSign_condition() == null || clue.getSign_condition().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getAudit_process() == null || clue.getAudit_process().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
        } else if (clue.getClue_type() == 2) {
            if (clue.getUnit_name() == null || clue.getUnit_name().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getName() == null || clue.getName().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getBusiness_scope() == null || clue.getBusiness_scope().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getMerchants_address() == null || clue.getMerchants_address().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getTel() == null || clue.getTel().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getWeb_address() == null || clue.getWeb_address().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
        } else if (clue.getClue_type() == 3) {
            if (clue.getName() == null || clue.getName().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getUnit_name() == null || clue.getUnit_name().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getBusiness_scope() == null || clue.getBusiness_scope().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }
            if (clue.getTel() == null || clue.getTel().isEmpty()) {
                throw new ServiceException(Constants.CODE_ERR_PARAM);
            }

        }
        clue.setAdd_time(DateUtil.getNowTimestamp());
        clueService.save(clue);

        Message message = new Message();
        message.setMessage_type(4);
        message.setMessage_content(member.getNickname() + "提交了线索");
        message.setMessage_port(2);
        message.setAdd_time(DateUtil.getNowTimestamp());
        messageService.save(message);


        return ResultGenerator.successResult();

    }

    //我的线索
    @GetMapping("/myClue")
    public Result myClue(PageBean<Map> page, Clue clue) {
        if (clue.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Map> list = clueService.selectMyClue(clue);
        for (int i = 0; i < list.size(); i++) {
            Double earnings = (Double) list.get(i).get("earnings");
            Double earnings2 = earnings / 2;
            list.get(i).put("earnings", earnings2.toString());
        }
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //线索详情
    @GetMapping("/clueDetail")
    public Result clueDetail(Clue clue) {
        if (clue.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Clue> list = clueService.selectClueById(clue);

        return ResultGenerator.successResult(list);
    }

}
